﻿using GreenPipes;
using MassTransit;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Config;
using NLog.Web;
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.OpenApi.Models;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using System.Net;
using System.Net.Http;

namespace Infrastructure.Swagger
{
    public static class SwaggerExtensions
    {
        public static void AddGoLifeSwagger(this IServiceCollection services, IHostEnvironment env)
        {
            ThreadPool.SetMinThreads(128, 128);
            ThreadPool.SetMaxThreads(512, 512);
            System.Net.ServicePointManager.DefaultConnectionLimit = 512;
            ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

            services.AddSwaggerGen(c =>
            {
                var arr = env.ApplicationName.Split(".");
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = arr[arr.Length - 1] + "服务文档"
                });
                var basePath = env.ContentRootPath;

                var xmlPath = Path.Combine(basePath, env.ApplicationName + ".xml");
                var xml2Path = Path.Combine(basePath, "DTO.xml");
                var xml3Path = Path.Combine(basePath, "Domain.xml");
                c.IncludeXmlComments(xmlPath);
                c.IncludeXmlComments(xml2Path);
                c.IncludeXmlComments(xml3Path);

                var bearerScheme = new OpenApiSecurityScheme
                {
                    Scheme = "Bearer",
                    BearerFormat = "JWT",
                    Description = "JWT Authorization",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.Http
                };
                c.AddSecurityDefinition("Bearer", bearerScheme);
                c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());

                c.AddSecurityRequirement(new OpenApiSecurityRequirement() {
                    {
                       new OpenApiSecurityScheme
                       {
                           Reference = new OpenApiReference
                           {
                               Type = ReferenceType.SecurityScheme,
                               Id = "Bearer"
                           }
                       },
                       new string[] { }
                   }
                });

                if (!env.ApplicationName.Equals("AdminApi"))
                {
                    c.OperationFilter<PublicHeaderParameter>();
                }
            });
        }
        public static void UseGoLifeSwagger(this IApplicationBuilder app)
        {
            Config.ApplicationConfig.SetConfig(app.ApplicationServices.GetService<IConfiguration>());
            Utility.HttpUtil._httpClientFactory = app.ApplicationServices.GetService<IHttpClientFactory>();

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Api");
                c.ShowExtensions();
               
            });

        }

    }
}
